import Game from "./game";
import ShapeManager from "./shapeManager";

const {ccclass, property} = cc._decorator;
export type RowColData = {
    row: number,
    col: number,
}
@ccclass
export default class GameController {
    gameMap: cc.Node[][] = [];
    shapeManager: ShapeManager = null;

    /** 落下的形状存储到一个map中以供日后消除使用 */
    stopedNodeMap: number[][] = [];

    private gridWidth: number = 32;

    // 游戏脚本
    game: Game = null;

    row: number = 30;
    col: number = 15;

    public init(): void {
        this.stopedNodeMap = [];
        for(let i = 0; i < this.row; i++) {
            this.stopedNodeMap[i] = [];
            for(let j = 0; j < this.col; j++) {
                this.stopedNodeMap[i][j] = 0;
            }
        }
    }

    public getRowByPoint(point: cc.Vec2): RowColData {
        let  res = {
            row: -1,
            col: -1
        }
        const col = Math.round(Math.floor((point.x + 224) / this.gridWidth));
        const row = Math.round(Math.floor(-(point.y + 16) / this.gridWidth));
        res.row = row;
        res.col = col;
        return res;
    }

    public getMapLocalPos(row: number,col: number): cc.Vec2 {
        const res: cc.Vec2 = cc.v2(0,0);
        res.x = -224 + col * this.gridWidth;
        res.y = -16 - row * this.gridWidth;
        return res;
    }

    /** 查看一个节点的位置超出地图的范围了没有 */
    public isInMap(pos: cc.Vec2): boolean {
        const rowColObj = this.getRowByPoint(pos);
        
        return rowColObj.row >= 0 && rowColObj.row <= this.row - 1 && rowColObj.col >= 0 && rowColObj.col <= this.col - 1;
    }

    public isInMapWithRowColObj(rowColObj: RowColData): boolean {
        return rowColObj.row >= 0 && rowColObj.row <= this.row - 1 && rowColObj.col >= 0 && rowColObj.col <= this.col - 1;
    }

}

export const gameController = new GameController();

